
<#
.SYNOPSIS 
Hydration Script for MDTEx

.DESCRIPTION
_TEMPLATE_

.NOTES
Copyright Keith Garner (KeithGa@DeploymentLive.com), All rights reserved.

.LINK
http://MDTEx.CodePlex.com

#>

[CmdletBinding()]
param(
    [switch] $Quiet,
    [parameter(mandatory=$true,HelpMessage="Location of Local Deployment Share.")]
    [string] $DeploymentLocalPath, # Example: c:\DeploymentShare
    [string] $DPDrive = "DS001",
    [parameter(ValueFromRemainingArguments=$true)] $Remaining
)

"Create Images with local Hyper-V" | Write-LogVerbose

$SwitchName = Get-VMSwitch -SwitchType External | Select-Object -First 1 -ExpandProperty Name

foreach ( $Name in get-childitem -path "$deploymentlocalPath\Control\*.cre" | select-object -ExpandProperty Basename | %{ $_.Replace(".CRE","") })
{
    $VHDPath = "$((get-vmHost).VirtualHardDiskPath)\$($Name).vhd"
    "Create Virtual Machine $Name at $VHDPath" | Write-LogVerbose

    remove-item $VHDPath -ErrorAction SilentlyContinue
    stop-vm -Name $Name -force -ErrorAction SilentlyContinue | Out-Null
    remove-vm -Name $Name -force -ErrorAction SilentlyContinue

    New-VHD -Path $VHDPath -SizeBytes 80GB | out-string | Write-Verbose
    $NewVM = New-VM -Name $Name -SwitchName $SwitchName -VHDPath $VHDPath
    Set-VM -VM $NewVM -ProcessorCount 4 -DynamicMemory -MemoryStartupBytes 3GB -MemoryMinimumBytes 3GB
    set-VMDVDDrive -VMName $NewVM.Name -Path "$DeploymentLocalPath\boot\LitetouchPE_Automated_x86.iso" 

    $uuid = (get-wmiobject -Namespace "Root\virtualization\v2" -class Msvm_VirtualSystemSettingData -Property BIOSGUID -Filter ("InstanceID = 'Microsoft:{0}'" -f $NewVM.VMId.Guid)).BIOSGUID.SubString(1,36)
    Set-MDTCustomSettings -DPShare $DeploymentLocalPath -Category $uuid -Key "TaskSequenceID" -Value "$($Name).CRE"
    Set-MDTCustomSettings -DPShare $DeploymentLocalPath -Category $uuid -Key "BackupFile" -Value "$($Name).wim"

    CheckPoint-VM -VM $NewVM -SnapShotName "Empty Virtual Machine"
    Start-VM -VM $NewVM
}

Start-Sleep 10


